package com.example.john.myapplication;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

/**
 * Created by john on 17/4/13.
 */

public class UserAdapter extends ArrayAdapter<User>{
    private Context context;
    private int resource;
    private List<User> users;

    public UserAdapter(Context context, int resource, List<User> objects) {
        super(context, resource, objects);
        this.context=context;
        this.resource=resource;
        this.users=objects;

    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final User u=users.get(position);

        convertView= LayoutInflater.from(context).inflate(resource,parent,false);
        TextView idTv=(TextView)convertView.findViewById(R.id.id_textView);
        TextView nameTv=(TextView)convertView.findViewById(R.id.name_textView);
        TextView ageTv=(TextView)convertView.findViewById(R.id.age_textView);
        Button delBtn=(Button)convertView.findViewById(R.id.del_btn);
        delBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(){
                    @Override
                    public void run() {
                        HttpURLConnection con=null;
                        OutputStream out=null;
                        BufferedReader reader=null;

                        try {
                            URL url=new URL("http://192.168.21.88:3000/users/"+u.getId());
                            con=(HttpURLConnection)url.openConnection();
                            con.setRequestMethod("DELETE");
                            out=con.getOutputStream();
                            //getInputstream ,connect to net!!!!
                            InputStream in=con.getInputStream();
                            users.remove(u);
                            Activity activity=(Activity)context;
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    UserAdapter.this.notifyDataSetChanged();
                                }
                            });

                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            con.disconnect();
                        }


                    }
                }.start();



            }
        });

        idTv.setText(u.getId()+"");
        nameTv.setText(u.getName());
        ageTv.setText(u.getAge()+"");
        return convertView;
    }
}
